package com.baiyue.admin.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;







import com.alibaba.fastjson.JSONObject;
import com.baiyue.admin.common.MessageResult;
import com.baiyue.admin.entity.BookInfo;
import com.baiyue.admin.manage.BookInfoManage;
import com.sun.org.apache.bcel.internal.generic.NEW;

@Controller
public class BookMngAct 
{
	private final static Logger LOGGER = Logger.getLogger(BookMngAct.class);
	@Autowired
	private BookInfoManage bookInfoManage;
	
	@RequestMapping("/bookMng/index.do")
	public ModelAndView index()
	{
		return new ModelAndView("/bookMng/index");
		
	}
	
	//显示功能
	@RequestMapping("/bookMng/list.do")
	public void list(HttpServletRequest request,HttpServletResponse response){
		
		//取数据
		List<BookInfo> results=bookInfoManage.getAllBookInfo();
		if(results == null || results.size() ==0){
			return; 
		}else{
			try {
				response.getWriter().print(JSONObject.toJSONString(results));
			} catch (IOException e) {
				LOGGER.error("写入失败.");
				e.printStackTrace();
			}
		}
		
	}
	
	//删除功能
	@RequestMapping("/bookMng/deleteBook.do")
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException{
		//验证
		/*MessageResult result = PermissionUtils.checkPermission(request, Operation.DELETE_BOOK_ADMIN);
		if(!result.isSuccess()){
			response.getWriter().print(result.marsh());
			return;
		}*/
		//取数据
		String bookId = request.getParameter("bookId");
		System.out.println("bookId:"+bookId);
		boolean success = bookInfoManage.deleteBook(bookId);
		MessageResult result = new MessageResult();
		result.setSuccess(success);
		if(success){
			result.setMessage("删除成功");
		}else{
			result.setMessage("删除失败");
		}
		response.getWriter().print(result.marsh());
	}
	
}
